$(function () {
	initProject();
});
function getbaseList(init){
	var projectId=$("#projectId").val();
	if(null==projectId || projectId==""){
		JY.Model.info("项目必选！");
		return;
	}
	if(init==1)$("#baseForm.pageNum").val(1);
	JY.Model.loading();
	
	JY.Ajax.doRequest("baseForm",jypath +'/customer/distribution/list',null,function(data){
		 $("#baseTable tbody").empty();
		 var page=data.obj;
		//数据集合
    	 var results=page.rows;
    	 
    	 //分页数据
     	 var pageNum=page.pageNumber,pageSize=page.pageSize,totalRecord=page.total;
    	 var html="";
    		 if(results!=null&&results.length>0){
        		 var leng=(pageNum-1)*pageSize;//计算序号
        		 for(var i = 0;i<results.length;i++){
            		 var l=results[i];
            		 html+="<tr>";
            		 html+="<td class='center'>"+(i+leng+1)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.customerName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.customerPhone)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.customerSexName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.intentionLevelName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.customerStatusName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.processStatusName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.processResult)+"</td>";
            		 html+="<td class='center'>"+JY.Date.Default(l.processTime)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.assignPersonName)+"</td>";
            		 html+="<td class='center'>"+JY.Object.notEmpty(l.createUserName)+"</td>";
            		 html+="<td class='center'>"+JY.Date.Default(l.distributionTime)+"</td>";
            		 html+="</tr>";		 
            	 } 
        		 $("#baseTable tbody").append(html);
        		 JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getbaseList");
        	 }else{
        		html+="<tr><td colspan='12' class='center'>没有相关数据</td></tr>";
        		$("#baseTable tbody").append(html);
        		$("#pageing ul").empty();//清空分页
        	 }	 
    	    JY.Model.loadingClose();
	 });
}

function getwfpList(init){
	var projectId=$("#wfpProjectId").val();
	if(null==projectId || projectId==""){
		JY.Model.info("项目必选！");
		return;
	}
	
	if(init==1)$("#baseForm .pageNum").val(1);	
	JY.Model.loading();

	JY.Ajax.doRequest("baseForm",jypath +'/call/callCustomer/getWeifenpeiPageModel',null,function(data){
		 $("#wfpTable tbody").empty();
		 var page=data.obj;
		//数据集合
    	 var results=page.rows;
    	 
    	 //分页数据
     	 var pageNum=page.pageNumber,pageSize=page.pageSize,totalRecord=page.total;
    	 var html="";
    		 if(results!=null&&results.length>0){
        		 var leng=(pageNum-1)*pageSize;//计算序号
        		 for(var i = 0;i<results.length;i++){
            		 var l=results[i];
            		 html+="<tr>";
            		 html+="<td class='center' style='vertical-align:middle;'><label><input type='checkbox' name='checkbox' class='ace' value='"+l.id+"'><span class='lbl'></span></label></td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+(i+leng+1)+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+JY.Object.notEmpty(l.userName)+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+JY.Object.notEmpty(l.phone)+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>call客</td>";
            		 html+="<td style='width:30%'>";
            		 html+="关注问题："+JY.Object.notEmpty(l.concern)+"<br/>";
            		 html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;备注："+JY.Object.notEmpty(l.remark)+"<br/>";
            		 html+="到访时间："+JY.Date.Format(l.visitTime, "yyyy-MM-dd");
            		 html+="</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+JY.Date.Format(l.lastCallTime, "yyyy-MM-dd")+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+JY.Object.notEmpty(l.createUserName)+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>"+JY.Date.Format(l.createTime, "yyyy-MM-dd")+"</td>";
            		 html+="<td class='center' style='vertical-align:middle;'>";
            		 html+="<a onclick='openModel(\""+l.id+"\")' style='cursor:pointer'>分配</a>";
            		 html+="</td>";
            		 html+="</tr>";		 
            	 } 
        		 $("#wfpList tbody").append(html);
        		 JY.Page.setPage("baseForm","wfpPageing",pageSize,pageNum,totalRecord,"getwfpList");
        	 }else{
        		html+="<tr><td colspan='12' class='center'>没有相关数据</td></tr>";
        		$("#wfpTable tbody").append(html);
        		$("#wfpPageing ul").empty();//清空分页
        	 }	 
    	    JY.Model.loadingClose();
    	    //查询销售分组
    	    xszList();
	 });
}

function ininUser(){
	$.ajax({
		type: 'post',
        url: jypath+"/system/user/findRoleUser",
	    data: {
	    },
	    dataType:'json',
	    success: function(res){
	    	$("#createUserId").empty();
	    	var list=res.obj;
	    	var option='<option value="" >全部</option>';
    		$.each(list, function(index, row) {
    			option=option+'<option value="'+row.id+'">'+row.userName+'</option>';
    		});
    		$("#createUserId").html(option);
	    	
	    }
	});
}

function findSummaryRate(){
	JY.Ajax.doRequest("baseForm",jypath +'/call/importStatistics/findSummaryRate',null,function(data){
		var map=JSON.parse(data.obj);
		var successRate=map.successRate;
		var effectiveRate=map.effectiveRate;
		if(null!=successRate && successRate!=""){
			option.series[0].data[0].value = successRate;
			mainChart.setOption(option, true);
		}
		if(null!=effectiveRate && effectiveRate!=""){
			option1.series[0].data[0].value = effectiveRate;
			main1Chart.setOption(option1, true);
		}
	});
}
function setProcessStatus(value){
	//$("#processStatus").val(value);
	if(value == "0"){
		$("#wfp").attr("style","");
		$("#wfpList").attr("style","");
		$("#yifenpei").attr("style","display:none");
		$("#yifenpeiList").attr("style","display:none");
		getwfpList(1);
	}else if(value == "1"){
		$("#wfp").attr("style","display:none");
		$("#wfpList").attr("style","display:none");
		$("#yifenpei").attr("style","");
		$("#yifenpeiList").attr("style","");
		getbaseList(1);
	}
}
function initProject(){
	var url=jypath+"/customer/follow/returnProjectName?resourcesCode=menu_0003_0001&resourcesName=resName_pro_list";
	$.ajax({
		type: 'post',
        url: url,
	    data: {
	    	resourcesCode:'menu_0003_0001',
	    	resourcesName:'resName_pro_list'
	    },
	    dataType:'json',
	    success: function(res){
	    	var list=res;
	    	var option='<option value="">全部</option>';
    		$.each(list, function(index, row) {
    			if(index==0){
    				option=option+'<option selected value="'+row.id+'">'+row.projectName+'</option>';
    			}else{
    				option=option+'<option value="'+row.id+'">'+row.projectName+'</option>';
    			}
    			
    		});
    		
    		initAllUser();
			$("#projectId").empty();
    		$("#projectId").html(option);
    		
    		$("#wfpProjectId").empty();
    		$("#wfpProjectId").html(option);
    		
    		if($("#processStatus").val() == "0"){
    			getwfpList(1);
    		}else if($("#processStatus").val() == "1"){
    			getbaseList(1);
    		}
    		
	    }
	});
}

function initAllUser(){
	var projectId=$("#projectId").val();
	var url=jypath+"/customer/customer/returnGroupUserList";
	$.ajax({
		type: 'post',
        url: url,
	    data: {
	    	resourcesCode:'menu_0006_0001',
	    	projectId:projectId
	    },
	    dataType:'json',
	    success: function(res){
	    	var list=res;
	    	var option='<option value="">全部</option>';
    		$.each(list, function(index, row) {
    			option=option+'<option value="'+row.id+'">'+row.userName+'</option>';
    		});
    		$("#createUserId").empty();
    		$("#assignPerson").empty();
    		$("#createUserId").html(option);
    		$("#assignPerson").html(option);
	    	
	    }
	});
}
//批量分配
function openModelList(){
	$("#fenPeiId").val(""); 
	
	var ids="";
	$("input[name='checkbox']").each(function(){
	     if($(this).is(':checked')){
	    	 if(ids==""){
	    		 ids=$(this).attr("value");
	    	 }else{
	    		 ids=ids+","+$(this).attr("value");
	    	 }
	   	 }
	});
	if(ids==""){
		layer.alert('请勾选需分配的客户');
		return;
	}
	$("#fenPeiId").val(ids); 
	//显示模态框
	$('#myModal').modal('show');
}
//打开分配框
function openModel(id){
	$("#fenPeiId").val(id);
	//显示模态框
	$('#myModal').modal('show');
}
//查询销售组
function xszList(){
	var projectId=$("#wfpProjectId").val();
	$.ajax({
		type: 'post',
        url: jypath+'/customer/customer/returnGroupList',
	    data: {
	    	"resourcesCode":"menu_0006_0001",
	    	"projectId":$("#wfpProjectId").val()
	    },
	    dataType:'json',
	    success: function(res){
	    	var option='';
    		for(var i = 0; i < res.length; i++){
    			option=option+'<option value="'+res[i].id+'">'+res[i].name+'</option>';
    		}
    		$("#xszId").empty();
    		$("#xszId").html(option);
    		//查询经纪人
    		jjList();
	    }
	});
}
//查询经纪人
function jjList(){
	var id = $("#xszId").val();
	$.ajax({
		type: 'post',
        url: jypath+'/customer/customer/returnGroupUserList',
	    data: {
	    	"resourcesCode":"menu_0006_0001",
	    	"salesGroupId":id
	    },
	    dataType:'json',
	    success: function(res){
	    	var option='';
    		for(var i = 0; i < res.length; i++){
    			option=option+'<option value="'+res[i].id+'">'+res[i].userName+'</option>';
    		}
    		$("#jjId").empty();
    		$("#jjId").html(option);
	    }
	});
}

//分配
function fenPei(){
	var ids = $("#fenPeiId").val();
	var projectId=$("#wfpProjectId").val();
	$.ajax({
		type: 'post',
        url: jypath+'/customer/distribution/distributionConsultant',
	    data: {
	    	"ids":ids,
	    	"projectId":projectId,
	    	"customerId":$("#jjId").val()
	    },
	    dataType:'json',
	    success: function(res){
	    	if(res.obj.success == true){
	    		$('#myModal').modal('hide');
	    		layer.msg("分配成功");
	    		getwfpList(1); //刷新列表
	    	}else{
	    		layer.alert("分配失败");
	    	}
	    }
	});
}
